package com.es.util;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.sniff.SniffOnFailureListener;
import org.elasticsearch.client.sniff.Sniffer;

public class HighLevelClient {

    private static RestClientBuilder restClientBuilder = ClientBuilder.restClientBuilder();

    private static RestHighLevelClient highLevelClient;

    private static Sniffer sniffer;

    public static RestHighLevelClient getRestHighLevelClient() {
        highLevelClient = new RestHighLevelClient(restClientBuilder);

        sniffer = Sniffer.builder(highLevelClient.getLowLevelClient())
                .setSniffIntervalMillis(60000)
                .setSniffAfterFailureDelayMillis(30000)
                .build();
        return highLevelClient;
    }

    public static void closeClient() {
        try {
            if (null != sniffer) {
                sniffer.close();
            }
            if (null != highLevelClient) {
                highLevelClient.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
